//
//  WBOAuthViewController.m
//  WeiBoOC
//
//  Created by 韩菲菲 on 16/8/10.
//  Copyright © 2016年 韩菲菲. All rights reserved.
//

#import "WBOAuthViewController.h"
#import "SVProgressHUD.h"
#import "WBUserAccountViewModel.h"

@interface WBOAuthViewController ()<UIWebViewDelegate>

@end

@implementation WBOAuthViewController

- (void)loadView
{
    
    //创建webview
    UIWebView *webView = [[UIWebView alloc]init];
    
    webView.delegate = self;
    
    self.view = webView;
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.weibo.com/oauth2/authorize?client_id=%@&redirect_uri=%@",WBAppKey,WBRedirect_uri]]];
    
    //加载界面
    [webView loadRequest:request];
    
    
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
}

- (void)setupUI
{
    self.view.backgroundColor = [UIColor whiteColor];
    //设置导航条
    self.navigationItem.title = @"微博登录";
    
    //设置左边按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancelAction)];
    
    //设置右边按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"自动填充" style:UIBarButtonItemStylePlain target:self action:@selector(autoFillAction)];
    
}

//自动填充按钮点击事件
- (void)autoFillAction
{
    //实现自动填充
    NSString *jsString = [NSString stringWithFormat:@ "document.getElementById('userId').value='%@',document.getElementById('passwd').value='%@'",WBName,WBPassWd];
    
    //注入js
    [(UIWebView *)self.view stringByEvaluatingJavaScriptFromString:jsString];
    
}

//取消按钮点击事件
- (void)cancelAction
{
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [SVProgressHUD show];
    
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [SVProgressHUD dismiss];
    
}

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    NSLog(@"加载失败%@",error);
    
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
  
    //判断哪个请求参数里面是带有code参数的，把code截取下来
    if ([request.URL.absoluteString hasPrefix:WBRedirect_uri])
    {
        
        NSString *code = [request.URL.query substringFromIndex:5];
        
        //根据获取到得code请求token
        [[WBUserAccountViewModel sharedWBUserAccountViewModel] getUserTokenWithCode:code andFinishedBlock:^(BOOL isSuccess) {
            
            if(!isSuccess){
            
                NSLog(@"数据没有获取到");
                
                return ;
            }
            
            NSLog(@"数据获取成功");
            
            [self dismissViewControllerAnimated:NO completion:^{
                
                [SVProgressHUD dismiss];
                //发送通知
                [[NSNotificationCenter defaultCenter] postNotificationName:WBSwitchRootViewController object:@"welcome"];
                
            }];
            
        }];
        
        return NO;
    }

    return YES;
}




@end
